
#include <stdint.h>
#include "Hal_NixieTube.h"
#include "Switch.h"
#include "Keyboard.h"
#include "../Vertical/APP_Main.h"


const GPIO_Type lcd_io[] = {IO_Tube_Seg0,IO_Tube_Seg1,IO_Tube_Seg2,IO_Tube_Seg3,IO_Tube_Seg4,IO_Tube_Seg5,IO_Tube_Seg6,IO_Tube_Seg7};
typedef enum
{
    tube_seg1 = 1,
    tube_seg2 = 2,
    tube_seg3 = 5,
    tube_seg4 = 4,
    tube_seg5 = 6,
    tube_seg6 = 0,
    tube_seg7 = 3,
    tube_seg8 = 7,
}_tube_seg;

#define NT_Shine_Gap 125 //66 //闪耀时刻计数 = 闪耀时间(400ms) / 单个数码管刷新时间6ms
#define NT_Shine_Count NT_Shine_Gap * 10 //66(闪耀时刻计数) * 9, 整个闪耀过程的时刻计数; 将时间模拟量转换成数字量

uint16_t Factory_Show_Num;
uint16_t Show_Count;
uint8_t show_shine;
uint32_t NT_None_Clock = 0;
uint8_t NT_None_Enable = 0; //1: Enable None
uint32_t Tube_Show = 0;


//通过抽象数码管构建的段码
const uint8_t lcd_code[] =
{
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg3) | (1 << tube_seg4) | (1 << tube_seg5) | (1 << tube_seg6)), //0
    ((1 << tube_seg2) | (1 << tube_seg3)),
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg7) | (1 << tube_seg5) | (1 << tube_seg4)), //2
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg7) | (1 << tube_seg3) | (1 << tube_seg4)), //3
    ((1 << tube_seg6) | (1 << tube_seg7) | (1 << tube_seg2) | (1 << tube_seg3)), //4
    ((1 << tube_seg1) | (1 << tube_seg6) | (1 << tube_seg7) | (1 << tube_seg3) | (1 << tube_seg4)), //5
    ((1 << tube_seg1) | (1 << tube_seg6) | (1 << tube_seg5) | (1 << tube_seg4) | (1 << tube_seg3) | (1 << tube_seg7) ), //6
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg3)), //7
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg3) | (1 << tube_seg4) | (1 << tube_seg5) | (1 << tube_seg6) | (1 << tube_seg7)), //8
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg3) | (1 << tube_seg4) | (1 << tube_seg6) | (1 << tube_seg7)), //9
    (1 << tube_seg8), //. 或者是 C,F
    ((1 << tube_seg1) | (1 << tube_seg4) | (1 << tube_seg5) | (1 << tube_seg6) | (1 << tube_seg7)), //E
    ((1 << tube_seg5) | (1 << tube_seg7)),  //r,12
    0x00, //NULL,数码段全灭, 13
    ((1 << tube_seg3) | (1 << tube_seg5) | (1 << tube_seg7)), //n
    ((1 << tube_seg5) | (1 << tube_seg7) | (1 << tube_seg3) | (1 << tube_seg4)), //小写 O
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg5) | (1 << tube_seg6) | (1 << tube_seg7)), //p, 16
    ((1 << tube_seg4) | (1 << tube_seg5) | (1 << tube_seg6)), //L
    ((1 << tube_seg2) | (1 << tube_seg3) | (1 << tube_seg5) | (1 << tube_seg6) | (1 << tube_seg7)), //H, 18
    ((1 << tube_seg7)), //-, 19
    ((1 << tube_seg1) | (1 << tube_seg5) | (1 << tube_seg6) | (1 << tube_seg7)), //F, 20
    ((1 << tube_seg1) | (1 << tube_seg3) | (1 << tube_seg4) | (1 << tube_seg6) | (1 << tube_seg7)), //S, 21
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg3) | (1 << tube_seg4) | (1 << tube_seg5) | (1 << tube_seg6)), //大写 O,22
    ((1 << tube_seg1) | (1 << tube_seg2) | (1 << tube_seg5) | (1 << tube_seg6) | (1 << tube_seg7)), //大写 P, 23
    ((1 << tube_seg1) | (1 << tube_seg4) | (1 << tube_seg5) | (1 << tube_seg6)), //C, 24
    ((1 << tube_seg3) | (1 << tube_seg4) | (1 << tube_seg5)), //v, 25
    ((1 << tube_seg1) | (1 << tube_seg2)), //:, 26
};
#define Tube_Bars 19 //-
uint8_t icon_Bars = 0;


void NT_GPIO_Init(void)
{
    GPIO_Init(GPIOA0,IO_Tube_Seg0,Output);
    GPIO_Init(GPIOA0,IO_Tube_Seg1,Output);
    GPIO_Init(GPIOA0,IO_Tube_Seg2,Output);
	GPIO_Init(GPIOA0,IO_Tube_Seg3,Output);
    GPIO_Init(GPIOB0,IO_Tube_Seg4,Output);
    GPIO_Init(GPIOB0,IO_Tube_Seg5,Output);
	GPIO_Init(GPIOB0,IO_Tube_Seg6,Output);
    GPIO_Init(GPIOA0,IO_Tube_Seg7,Output);
    GPIO_Init(GPIOB0,IO_Tube_Com0,Output);
	GPIO_Init(GPIOA0,IO_Tube_Com1,Output);
    GPIO_Init(GPIOA0,IO_Tube_Com2,Output);
    GPIO_Init(GPIOA0,IO_Tube_Com3,Output);
}
void NT_GPIO_Write(uint8_t seq,uint8_t level)
{
	CSP_GPIO_T* temp;
	
	temp = GPIOA0;
	if((seq == 4) || (seq == 5) || (seq == 6)) temp = GPIOB0;
	if(level == 0) GPIO_Write_Low(temp,lcd_io[seq]);
	else GPIO_Write_High(temp,lcd_io[seq]);
}

//seq: 0:百位,1:十位,2:个位
//num: BCD格式
void NT_NumsShow(uint8_t seq,uint32_t num)
{
    uint8_t i;
    uint32_t temp;

    if(NT_None_Enable > 0)
    {
        Com0_H;
        Com1_H;
        Com2_H;
        Com3_H;

        return;
    }

    if(seq == 0)
    {
        Com0_L;
        Com1_H;
        Com2_H;
        Com3_H;
        temp = ((num >> 16) & 0xff);
        if(UI_System.ShutdownCount == 0)
        {
            if((temp == 0) && (Ver_Enable == 0))
            {
                temp = Tube_Encoder_None;
                if(icon_Bars > 0) temp = Tube_Bars;
                else Com0_H;
            }
            if(Ver_Enable > 0)
            {
                if((num == Tube_Encoder_v) || (num == (Tube_Encoder_v << 8 | Tube_Encoder_F)) || (num == (Tube_Encoder_v << 8 | Tube_Encoder_C)))
                {
                    temp = Tube_Encoder_None;
                    Com0_H;
                }
            }
        }
    }
    else if(seq == 1)
    {
        Com0_H;
        Com1_L;
        Com2_H;
        Com3_H;
        temp = ((num >> 8) & 0xff);
        if(UI_System.ShutdownCount == 0)
        {
            if((((num >> 16) & 0xff) == 0) && (temp == 0) && (Ver_Enable == 0))
            {
                temp = Tube_Encoder_None;
                Com1_H;
            }
            if(Ver_Enable > 0)
            {
                if(num == Tube_Encoder_v)
                {
                    temp = Tube_Encoder_None;
                    Com1_H;
                }
            }
        }
    }
    else if(seq == 2)
    {
        Com0_H;
        Com1_H;
        Com2_L;
        Com3_H;
        temp = (num & 0xff);
    }
    else if(seq == 3)
    { //:
        if(UI_System.UI_Show_Colon == UI_ON)
        {
            Com0_H;
            Com1_H;
            Com2_H;
            Com3_L;
            //Seg1_H, Seg2_H 
            temp = (Tube_Encoder_Colon & 0xff); //:
        }
        else
        {
            Com0_H;
            Com1_H;
            Com2_H;
            Com3_H;
        }
    }

    for (i = 0; i <= 7; i++)
    {
        NT_GPIO_Write(i,((lcd_code[temp] >> i) & 0x01));
    }
}

void NT_Shine_Stop(void)
{
    NT_None_Clock = 0;
    NT_None_Enable = 0;
    switch_trigger_Flag = 0;
    key_mset_state = 0;
}
void NT_Shine_Rest(void)
{
    NT_None_Enable = 0;
    NT_None_Clock = NT_Shine_Count;
}

void NT_CharsSettings(uint32_t num)
{
    Tube_Show = num;
}
void NT_NumsSettings(int32_t num)
{
    uint32_t temp;

    if(num < -50) num = -50;
    if(num < 0)
    {
        num = -num;
        temp = 0;
        icon_Bars = 1; 
    }
    else
    {
        temp = ((num / 100) % 10);
        icon_Bars = 0; 
    } 
    temp <<= 8;
    temp |= ((num / 10) % 10);
    temp <<= 8;
    temp |= (num % 10);
    
    NT_CharsSettings(temp);
}

void NT_Init(void)
{
    uint32_t temp;

    NT_GPIO_Init();

//默认数码管不显示
    Com0_L;
    Com1_L;
    Com2_L;
    Com3_H;

    Seg0_L;
    Seg1_L;
    Seg2_L;
    Seg3_L;
    Seg4_L;
    Seg5_L;
    Seg6_L;
    Seg7_L;

    temp = Tube_Encoder_None;
    temp <<= 8;
    temp |= Tube_Encoder_None;
    temp <<= 8;
    temp |= Tube_Encoder_None;
    NT_CharsSettings(temp);
}

void NT_Main(void)
{
}

//Tick 2ms
void Tube_Clock(void)
{
    static uint8_t seq = 0;

    if(Show_Count < 0xffff) Show_Count++;
    if(Show_Count >= 2)
    {
        Show_Count -= 2;
        if(NT_None_Clock > 0)
        {
            NT_None_Clock--;
            if((NT_None_Clock % NT_Shine_Gap) == 0)
            {
                NT_None_Enable = 1 - NT_None_Enable;
            }
            if(NT_None_Clock == 0)
            {
                NT_Shine_Stop();
            }
        }
        NT_NumsShow(seq,Tube_Show);
        seq++;
        if(seq >= 4) seq = 0;
    }
}
